﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Data;

using InfFront.InfoSources.Base;

namespace InfFront.InfoSources.ADO
{
    [Base.Design.InfoSourceDesignTimeExtension(typeof(Design.DataTableInfoSourceDesignTimeExtension))]
    public partial class DataTableInfoSource : AbstractListInfoSource
    {
        #region Private Variables
        private DataTable dataTable = null;
        #endregion Private Variables

        #region Construction/Initialization
        public DataTableInfoSource()
        {
            Init();
        }

        public DataTableInfoSource(IContainer container)
        {
            container.Add(this);

            Init();
        }

        private void Init()
        {
            InitializeComponent();
        }
        #endregion Construction/Initialization

        #region Public Properties

        [Category("General"), DefaultValue(null), Description("Source data table reference")]
        public DataTable DataTable
        {
            get { return dataTable; }
            set { dataTable = value; }
        }

        #endregion Public Properties

        #region Internal interface

        protected override System.Collections.IList GetInternalList()
        {
            if (DataTable == null)
                return null;
            return new DataRowCollectionIList(DataTable.Rows);
        }

        protected override object CreateEmptyItem()
        {
            return DataTable.NewRow();
        }

        #endregion Internal interface

    }

    internal class DataRowCollectionIList : System.Collections.IList
    {
        private DataRowCollection rows;

        public DataRowCollectionIList(DataRowCollection rows)
        {
            this.rows = rows;
        }

        #region IList Members

        public int Add(object value)
        {
            rows.Add((DataRow)value);
            return rows.Count - 1;
        }

        public void Clear()
        {
            rows.Clear();
        }

        public bool Contains(object value)
        {
            return IndexOf(value) != -1;
        }

        public int IndexOf(object value)
        {
            return rows.IndexOf((DataRow)value);
        }

        public void Insert(int index, object value)
        {
            rows.InsertAt((DataRow)value, index);
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Remove(object value)
        {
            rows.Remove((DataRow)value);
        }

        public void RemoveAt(int index)
        {
            rows.RemoveAt(index);
        }

        public object this[int index]
        {
            get
            {
                return rows[index];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            rows.CopyTo(array, index);
        }

        public int Count
        {
            get { return rows.Count; }
        }

        public bool IsSynchronized
        {
            get { return rows.IsSynchronized; }
        }

        public object SyncRoot
        {
            get { return rows.SyncRoot; }
        }

        #endregion

        #region IEnumerable Members

        public System.Collections.IEnumerator GetEnumerator()
        {
            return rows.GetEnumerator();
        }

        #endregion
    }
}
